import $ from 'jquery';
import { NotificationManager } from 'react-notification';

class Request {
  constructor() {
    if(!(this instanceof Request)) {
      return new Request();
    }

    this.default = {
      dataType: 'json',
      success: function() {
        throw new Error('success handler undefined. ');
      },
      error: function(err) {
        NotificationManager.error(err || '服务器忙，请稍候重试');
      }
    };
  }

  prepareOptions(options, type) {
    return $.extend({}, this.default, options, {type: type});
  }

  get(options) {
    return $.ajax(this.prepareOptions(options, 'get'));
  }

  post(options) {
    return $.ajax(this.prepareOptions(options, 'post'));
  }
};

let $req = new Request();

export default $req;
